﻿using CKS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS.AGV.Core
{
    public abstract partial class BasePlcAction
    {
        public static readonly object _lockforklift = new object();
        public void AddForkLiftTask(CksPlcDataPosition pos, string taskName, int StartPlcId, int EndPlcId, int taskType, int pointType)
        {
            lock (_lockforklift)
            {
                var endPoint = AgvCache.AllPoints.Find(a => a.PointType == pointType && a.StationId == EndPlcId && a.StationArea == pos.LocationId);
                var startPoint = AgvCache.AllPoints.Find(a => a.PointType == pointType && a.StationId == StartPlcId && a.StationArea == pos.LocationId);

                agvserver_tasks stask = new agvserver_tasks()
                {
                    Desc = taskName,
                    CarType = 1,//0潜伏式 1叉车
                    TaskType = taskType,
                    StartPathPoint = startPoint.Point,
                    EndPathPoint = endPoint.Point,
                    CallerPlcId = EndPlcId
                };

                //工站同一个任务类型只能存在一条未完成的任务。
                var one = repoServerTasks.Where(a => a.CallerPlcId == StartPlcId && a.TaskType == taskType && a.TaskStatus < 10).First();
                if (one == null)
                {
                    repoServerTasks.Insert(stask);
                }

                //保存完待匹配任务后，开始进行匹配操作
                var matchTaskType = taskType == 1 ? 2 : 1;
                var matchTask  = repoServerTasks.Where(a => a.TaskType == matchTaskType && a.StartPathPoint == stask.EndPathPoint && a.EndPathPoint == stask.StartPathPoint)
                    .First();

                if (matchTask != null)
                {
                    //匹配成功，生成输送任务\ 起点是取料 终点是送料
                    var conveyStartPoint = taskType == 1 ? startPoint : endPoint;
                    var conveyEndPoint = taskType == 1 ? endPoint : startPoint;
                  
                    agv_convey_tasks conveytask = new agv_convey_tasks()
                    {
                        CreatedTime = DateTime.Now,
                        StartPathPoint = conveyStartPoint.Point,
                        StartPlcId = taskType == 1 ? StartPlcId : EndPlcId,
                        Desc = $"叉车输送任务{conveyStartPoint.Point}-{conveyEndPoint.Point} ",
                        StartReqSignalAddr = conveyStartPoint.AgvModbusAddr,
                        StartRspSignalAddr = conveyStartPoint.UpperModbusAddr,
                        EndPathPoint = conveyEndPoint.Point,
                        EndPlcId = taskType == 1 ? EndPlcId : StartPlcId,
                        EndReqSignalAddr = conveyEndPoint.AgvModbusAddr,
                        EndRspSignalAddr = conveyEndPoint.UpperModbusAddr,
                        PartType = 0,
                        TaskType = 3,
                        IsTest = false,
                    };
                    repoTasks.Insert(conveytask);
                    //更新两条匹配任务的输送id
                    stask.ConveyId = conveytask.Id;
                    stask.TaskStatus = 1;
                    matchTask.ConveyId = conveytask.Id;
                    matchTask.TaskStatus = 1;
                    repoServerTasks.Update(stask);
                    repoServerTasks.Update(matchTask);

                }



            }
            



        }
    }
}
